﻿using System.ComponentModel;

namespace TimeKeeper.ViewModels
{
    public static class ViewModelHelper
    {
        public static bool IsValid<T>(T obj)
        {
            var errorInfo = obj as IDataErrorInfo;
            return errorInfo != null && errorInfo.Error == string.Empty;
        }

        public static T SetValueWithRegistration<T>(T newValue, T oldValue, PropertyChangedEventHandler eventHandler) where T : class
        {
            if (oldValue != null && oldValue.Equals(newValue)) return oldValue;
            var property = oldValue as INotifyPropertyChanged;
            if (property != null) property.PropertyChanged -= eventHandler;
            oldValue = newValue;
            property = oldValue as INotifyPropertyChanged;
            if (property != null) property.PropertyChanged += eventHandler;
            return oldValue;
        }
    }
}
